using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace MicroMsg.UI.UserContrl
{
	public class CircleMPageCommentItem : UserControl
	{
		private const string TAG = "CircleMPageCommentItem";
		private const int MAX_ELEMENT_NUM = 400;
		private const int cNMaxCacheCnt = 10;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(SnsComment), typeof(CircleMPageCommentItem), new PropertyMetadata(null, new PropertyChangedCallback(CircleMPageCommentItem.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		private NickNameButton mBtnNick = new NickNameButton();
		private bool mBIsBtnNickFromInit;
		private NickNameButton mBtnNickTo = new NickNameButton();
		private bool mBIsBtnNickToInit;
		private string mStrContent;
		private string lastContentCache;
		private string mStrLastNames;
		private static List<CircleMPageCommentItem> sListItems = new List<CircleMPageCommentItem>();
		internal Paragraph sParagraph;
		private bool _contentLoaded;
		public SnsComment ItemObject
		{
			get
			{
				return (SnsComment)base.GetValue(CircleMPageCommentItem.ItemObjectProperty);
			}
			set
			{
				base.SetValue(CircleMPageCommentItem.ItemObjectProperty, value);
			}
		}
		public CircleMPageCommentItem()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~CircleMPageCommentItem()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == CircleMPageCommentItem.ItemObjectProperty)
			{
				(dependencyObject_0 as CircleMPageCommentItem).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private void initBtnFromContainer()
		{
			if (this.mBIsBtnNickFromInit)
			{
				return;
			}
			this.mBIsBtnNickFromInit = true;
			this.mBtnNick.SetMode(NickNameButtonMode.WithColon);
			this.mBtnNick.txtNickName.FontSize = 20.0;
			this.mBtnNick.txtTag.set_FontSize(20.0);
			this.mBtnNick.set_VerticalAlignment(2);
			this.mBtnNick.set_Margin(new Thickness(0.0, 0.0, 5.0, -5.0));
		}
		private InlineUIContainer getNickFrom(string strUserName, bool isNeedColon = true)
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			this.initBtnFromContainer();
			if (this.mBtnNick.get_Parent() != null)
			{
				InlineUIContainer inlineUIContainer2 = this.mBtnNick.get_Parent() as InlineUIContainer;
				if (inlineUIContainer2 != null)
				{
					inlineUIContainer2.set_Child(null);
				}
			}
			if (isNeedColon)
			{
				this.mBtnNick.SetMode(NickNameButtonMode.WithColon);
			}
			else
			{
				this.mBtnNick.SetMode(NickNameButtonMode.Default);
			}
			this.mBtnNick.ItemObject = strUserName;
			inlineUIContainer.set_Child(this.mBtnNick);
			return inlineUIContainer;
		}
		private void initBtnToContainer()
		{
			if (this.mBIsBtnNickToInit)
			{
				return;
			}
			this.mBIsBtnNickToInit = true;
			this.mBtnNickTo.SetMode(NickNameButtonMode.WithColon);
			this.mBtnNickTo.txtNickName.FontSize = 20.0;
			this.mBtnNickTo.txtTag.set_FontSize(20.0);
			this.mBtnNickTo.set_VerticalAlignment(2);
			this.mBtnNickTo.set_Margin(new Thickness(0.0, 0.0, 0.0, -5.0));
		}
		private InlineUIContainer getNickTo(string strUserName)
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			this.initBtnToContainer();
			if (this.mBtnNickTo.get_Parent() != null)
			{
				InlineUIContainer inlineUIContainer2 = this.mBtnNickTo.get_Parent() as InlineUIContainer;
				if (inlineUIContainer2 != null)
				{
					inlineUIContainer2.set_Child(null);
				}
			}
			this.mBtnNickTo.ItemObject = strUserName;
			inlineUIContainer.set_Child(this.mBtnNickTo);
			return inlineUIContainer;
		}
		private Inline getTxtWhoToWho()
		{
			Run run = new Run();
			run.set_Text(strings.WC_COMMENT_TO_TA);
			return run;
		}
		private void RefreshItem()
		{
			SnsComment itemObject = this.ItemObject;
			if (itemObject == null)
			{
				return;
			}
			this.mStrContent = itemObject.strContent;
			this.RefreshRichText();
		}
		private void RefreshRichText()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mStrContent))
			{
				return;
			}
			string text = this.ItemObject.strUserName;
			if (!string.IsNullOrEmpty(this.ItemObject.strReplyUsername))
			{
				text = text + "@" + this.ItemObject.strReplyUsername;
			}
			if (this.mStrContent == this.lastContentCache && text == this.mStrLastNames)
			{
				return;
			}
			this.mStrLastNames = text;
			this.lastContentCache = this.mStrContent;
			this.sParagraph.get_Inlines().Clear();
			List<InLineResult> inLineResult = RichTextHelper.GetInLineResult(this.mStrContent, (int)this.sParagraph.get_FontSize(), null, null);
			bool flag = false;
			if (!string.IsNullOrEmpty(this.ItemObject.strReplyUsername))
			{
				flag = true;
			}
			if (flag)
			{
				InlineUIContainer inlineUIContainer = this.getNickFrom(this.ItemObject.strUserName, false);
				this.sParagraph.get_Inlines().Add(inlineUIContainer);
				this.sParagraph.get_Inlines().Add(this.getTxtWhoToWho());
				inlineUIContainer = this.getNickTo(this.ItemObject.strReplyUsername);
				this.sParagraph.get_Inlines().Add(inlineUIContainer);
			}
			else
			{
				InlineUIContainer nickFrom = this.getNickFrom(this.ItemObject.strUserName, true);
				this.sParagraph.get_Inlines().Add(nickFrom);
			}
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
		}
		public static CircleMPageCommentItem GetOneItem()
		{
			if (CircleMPageCommentItem.sListItems != null && CircleMPageCommentItem.sListItems.get_Count() != 0)
			{
				CircleMPageCommentItem result = Enumerable.First<CircleMPageCommentItem>(CircleMPageCommentItem.sListItems);
				CircleMPageCommentItem.sListItems.RemoveAt(0);
				return result;
			}
			return new CircleMPageCommentItem();
		}
		public static int GetCacheCnt()
		{
			if (CircleMPageCommentItem.sListItems == null)
			{
				return 0;
			}
			return CircleMPageCommentItem.sListItems.get_Count();
		}
		public static void PushOneItem(CircleMPageCommentItem item)
		{
			if (item == null)
			{
				return;
			}
			if (CircleMPageCommentItem.sListItems == null)
			{
				CircleMPageCommentItem.sListItems = new List<CircleMPageCommentItem>();
			}
			item.ClearMemory();
			if (item.get_Parent() != null)
			{
				return;
			}
			if (CircleMPageCommentItem.sListItems.get_Count() < 10)
			{
				CircleMPageCommentItem.sListItems.Add(item);
			}
		}
		public void ClearMemory()
		{
			this.ItemObject = null;
			this.mStrContent = null;
			this.lastContentCache = null;
			this.mStrLastNames = null;
			this.sParagraph.get_Inlines().Clear();
		}
		private void btnNick_Click(object sender, RoutedEventArgs e)
		{
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Circle/UI/CircleMPageCommentItem.xaml", 2));
			this.sParagraph = (Paragraph)base.FindName("sParagraph");
		}
	}
}
